<?php namespace App\Model;

//use App\Lib\Params\BaseWebParams;
use App\Lib\Params\WebColumnSearchParams;

class WebColumn extends BaseModel {

	//
    public function infos(){
        return $this->hasMany('App\Model\Info','volumn_id','id');
    }

    public static function search(WebColumnSearchParams $webColumnSearchParams)
    {
        $webColumnModel = self::select(array('id','column_name'))->where('is_used', '=', 0)->where('is_del', '=', 0);
        if(!empty($webColumnSearchParams->parentId)){
            $webColumnModel->where('parent_id', '=', $webColumnSearchParams->parentId);
        }
        if(!empty($webColumnSearchParams->businessId)){
            $webColumnModel->where('business_id' , '=', $webColumnSearchParams->businessId);
        }
        return $webColumnModel->orderBy('parent_id','asc')->get();
    }

    public static function getParentId($columnId)
    {
        if(empty($columnId) || !is_numeric($columnId)){
            return null;
        }
        $columnInfo = self::select('parent_id')->find($columnId);
        if(!isset($columnInfo->parent_id)){
            return null;
        }
        return $columnInfo->parent_id;
    }
}
